//
// Created by yuyou on 2023/11/27.
//

#ifndef UNTITLED1_THREADPOOL_H
#define UNTITLED1_THREADPOOL_H


#include <iostream>
#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <future>
class ThreadPool {
public:
    ThreadPool(size_t numThreads) :stop(false) {
        for (size_t i = 0; i < numThreads; i++) {
            workers.emplace_back(
                [this]() {
                    while (true) {
                        std::function<void()> task;
                        {
                            std::unique_lock<std::mutex> lock(this->queueMutex);
                            this->condition.wait(lock, [this] {return this->stop || !this->tasks.empty(); });

                            if (this->stop && this->tasks.empty()) {
                                return;
                            }

                            task = std::move(this->tasks.front());
                            this->tasks.pop();
                        }

                        task();
                    }
                }

            );
        }
    }


    //    template<class F, class... Args>
    //    void enqueue(F&& f, Args&&... args) {
    //    	{
    //    		std::unique_lock<std::mutex> lock(queueMutex);
    //    		tasks.emplace([=] {f(args...);});
    //    	}
    //    	condition.notify_one();
    //    }

    template<class F, class... Args>
    auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> {
        using return_type = typename std::result_of<F(Args...)>::type;

        auto task = std::make_shared<std::packaged_task<return_type()>>(
            std::bind(std::forward<F>(f), std::forward<Args>(args)...)
        );

        std::future<return_type> result = task->get_future();

        {
            std::lock_guard<std::mutex> lock(queueMutex);
            if (stop)
                throw std::runtime_error("enqueue on stopped threadpool");

            tasks.emplace([task]() {(*task)(); });
        }

        condition.notify_one();
        return result;

    }

    ~ThreadPool() {
        {
            std::unique_lock<std::mutex> lock(queueMutex);
            stop = true;
        }

        condition.notify_all();
        for (std::thread& worker : workers) {
            worker.join();
        }
    }

private:
    std::vector<std::thread> workers;
    std::queue<std::function<void()>> tasks;
    std::mutex queueMutex;
    std::condition_variable condition;
    bool stop;
};



#endif //UNTITLED1_THREADPOOL_H
